Return to doc.sitecore.com

How to make replacer avoid blob field in media item
Prev Next

Author: Ivan Sharamok
Posted: 6/5/2007 3:34:54 PM

Q: How to make replacer avoid BLOB data in media item if replacer is on?

 

A: You can create your own replacer derived from Sitecore.Text.Replacer class and override ReplaceFieldValues(Item item) method.

Then you should edit the following line in web.config file:

    <replacers>
      <replacer mode="on" id="publish" type="Sitecore.Text.CustomReplacer.AvoidFieldReplacer, your_assembly" singleInstance="true">

    ..............................

 

Here is the example of code:

 

using Sitecore.Collections;
using Sitecore.Data.Fields;
namespace Sitecore.Text.CustomReplacer
{
 
public class AvoidFieldReplacer : Sitecore.Text. Replacer
{
    public AvoidFieldReplacer( string name)
        : base(name)
    {

    }
    public override bool ReplaceFieldValues(Sitecore.Data.Items.Itemitem)
    {
        bool flag = false;
        bool isEditing = false;
        if (this.IsEmpty)
        {
            return false;
        }
        foreach (Field field in item.Fields)
        {
            if (field.Type.ToLower() == "attachment")
                {
                continue;
                }
            string fieldValue = field.Value;
            string newFieldValue = base.Replace(fieldValue);
            if (fieldValue != newFieldValue)
                {
                if (!item.Editing.IsEditing)
                    {
                        item.Editing.BeginEdit();
                        isEditing = true;
                    }
                    field.Value = newFieldValue;
                    flag = true;
                }
        }
        if (isEditing)
        {
            item.Editing.EndEdit();
        }
        return flag;
    }
}
}

 


Prev Next